﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string jinggao4;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["xinzengshu"] != "" || Session["xinzengshu"] != null)
        {
            if (Convert.ToInt32(Session["xinzengshu"]) <= 0)
            {
                Button1.Enabled = false;
                Button1.Text = "当月计划单已达10张，无法新增";
            }
        }
        if (!IsPostBack)
        {
            Label1.Text = Session["pnchejian"].ToString();
            Label2.Text = Session["pn"].ToString();
            Label3.Text = Session["pngeren"].ToString();

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (lei.SelectedValue == "请选择")
        {
            lei.Focus();
            string jinggao6 = "<SCRIPT language=javascript>alert('";
            jinggao6 += "请选择类别！";
            jinggao6 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa6", jinggao6);
            return;
        }
        if(DropDownListshenpibumen.SelectedValue=="请选择")
        {
            DropDownListshenpibumen.Focus();
            string jinggao5 = "<SCRIPT language=javascript>alert('";
            jinggao5 += "请选择审批部门！";
            jinggao5 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa5", jinggao5);
            return;
        }
        try
        {
            string sqlstr2 = "INSERT INTO cailiaoguanli ([lei],[shenpibumen],[chejian],[banzu],[addname],[addtime],[addip],[zt])VALUES(";
            sqlstr2 += "'" + lei.SelectedValue + "',";
            sqlstr2 += "'" + DropDownListshenpibumen.SelectedValue + "',";
            sqlstr2 += "'" + Label1.Text + "',";
            sqlstr2 += "'" + Label2.Text + "',";
            sqlstr2 += "'" + Label3.Text + "',";
            sqlstr2 += "'" + DateTime.Now.ToString() + "',";
            sqlstr2 += "'" + Page.Request.UserHostAddress.ToString().Trim() + "',";
            sqlstr2 += "0)";


            //Response.Write(sqlstr2);
            //Response.End();
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr2, sqlcon);
            sqlcon.Open();
            int upcnt = sqlcom.ExecuteNonQuery();
            if (upcnt == 1)
            {
                sqlstr2 = "Select scope_identity()";
                sqlcom = new SqlCommand(sqlstr2, sqlcon);
                SqlDataReader dr = sqlcom.ExecuteReader();
                if (dr.Read())
                {
                   string getid = dr.GetValue(0).ToString();
                   jinggao4 = "<SCRIPT language=javascript>document.location.href='bz_jihuatibao.aspx?id=" + getid + "';</SCRIPT>";
                   //string jinggao2 = "<SCRIPT language=javascript>alert('";
                   //jinggao2 += getid;
                   //jinggao2 += "');</SCRIPT>";
                   //ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao2);
                }
                dr.Close();
            }
            sqlcon.Close();
            string jinggao3 = "<SCRIPT language=javascript>alert('";
            jinggao3 += "新增计划单成功！将转入材料填报页面";
            jinggao3 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
            //转向语句
            ClientScript.RegisterStartupScript(this.GetType(), "aa4", jinggao4);
        }
        catch (SqlException)
        {
            throw;
        }


    }
}